/*
 * ClarkeCPE301DesignAssignment7.c
 *
 * Created: 5/2/2014 11:51:33 PM
 *  Author: Iguana
 */ 

#define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
#include <avr/pgmspace.h>

#include "nokia5110.h"		// LCD header file makes this a lot simpler

void init()
{
	LcdInit();						// Initialize LCD from nokia5110.h
	LcdContrast(0x7F);		// Set highest contrast for maximum visibility
	LcdClear();					// Clear LCD and enable updating
	ADCSRA = 0x87;			// ADC Enable with Clock/128
	ADMUX = 0x60;				// AVCC VRef with ADC0 for input
	DDRC = 0x00;				// PortC as an input
}

int convert()
{
	uint16_t Temperature;							// Temperature variable
	ADCSRA = 0xE7;									// Set ADSC to 1 and Start conversion
	while( !(ADCSRA & (1<<ADSC)) == 0);	// Sit for full conversion
	Temperature = (ADCH << 8)|ADCL;			// Save temperature
	return Temperature;
}

int main()
{
	init();		
	uint16_t Temperature;							// Temperature variable												// Initialize LCD and ADC
	uint16_t Tens_Temp; 								// Tens value of the temperature
	uint16_t Ones_Temp;								// Ones value of the temperature
	uint16_t Tenths_Temp;							// Tenths value of the temperature
	while (1)
	{
		Temperature = convert();											// Read the temperature and convert it
		Tens_Temp = Temperature%10;					// Calculate Tens value
		Ones_Temp = Temperature%100/10;				// Calculate Ones value
		Tenths_Temp = Temperature%1000/100;		// Calculate Tenths value
		char TemperatureArray[4];					// Array displaying all temperature digits
		sprintf(TemperatureArray, "%d%d.%d ", Tens_Temp, Ones_Temp, Tenths_Temp);
		LcdClear();										// Clear LCD and enable updating
		LcdGotoXYFont(1,1);							// Column 1 Row 1
		LcdFStr(FONT_1X,(unsigned char*)PSTR ("Brian C."));
		LcdGotoXYFont(1,2);							// Column 1 Row 2
		LcdFStr(FONT_1X,(unsigned char*)PSTR ("Fahrenheit"));
		LcdGotoXYFont(1,3);							// Column 1 Row 3
		LcdFStr(FONT_1X,(unsigned char*)TemperatureArray);
		LcdUpdate();										// Update LCD
		_delay_ms(500);								// Wait before updating again
	}
	return 0;
}